<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
/*
 *	Test de script puZZle
 *
*/
include './ZzL/autoload.php';
include_once './ZzL/Conf.php';
include_once './ZzL/Right.php';
include './ZzL/generation.php';
session_start();
$_SESSION['user'] = 1;
$_SESSION['group'][0] = 1;
$GLOBALS['cwd'] = getcwd();
//error_reporting(0);
$GLOBALS["tmpId"] = 1;
//$GLOBALS['MySql'] = new mysqli($GLOBALS['Serveur'], $GLOBALS['Login'], $GLOBALS['PassWord'], $GLOBALS['DB']);
?>
<?php
if(!empty($_GET['ZzS']))
	$_POST['ZzS'] = $_GET['ZzS'];
elseif(!empty($_SESSION['ZzS']) and empty($_POST['ZzS']))
	$_POST['ZzS'] = $_SESSION['ZzS'];
elseif(!isSet($_POST['ZzS']))
	$_POST['ZzS'] = '';

if(isSet($_POST['vlabel_id']))
	$_SESSION['vlabel_id'] = $_POST['vlabel_id'];
elseif(isSet($_SESSION['vlabel_id']))
	$_POST['vlabel_id'] = $_SESSION['vlabel_id'];
else
	$_GET['label'] = $_POST['vlabel_id'] = $_SESSION['vlabel_id'] = 0;

if(isSet($_POST['vlabel_id'])) {
	$_GET['label'] = $_POST['vlabel_id'];
	$_label = explode('-',$_POST['vlabel_id']);
	if($_label[0] != 0)
		$GLOBALS['label_id'] = $_label[0];
}
if(!isSet($_SESSION['vlabel_user']))
	$_SESSION['vlabel_user'] = '1';
if(empty($_SESSION['vlabel_group']))
	$_SESSION['vlabel_group'] = '1';
if(empty($_SESSION['vlabel_name']))
	$_SESSION['vlabel_name'] = 'label test';
if(empty($_SESSION['vlabel_type']))
	$_SESSION['vlabel_type'] = 'defaultType';
if(empty($_SESSION['vlabel_chmod']) or !preg_match('!^([0-1]){6}$!',$_SESSION['vlabel_chmod']))
	$_SESSION['vlabel_chmod'] = '111111';
	
/*if(is_null(@$_POST['vlabel_id']))
	$_POST['vlabel_id'] = $_SESSION['vlabel_id'];
else
	$_SESSION['vlabel_id'] = $_POST['vlabel_id'];*/
if(empty($_POST['vlabel_user']))
	$_POST['vlabel_user'] = $_SESSION['vlabel_user'];
else
	$_SESSION['vlabel_user'] = $_POST['vlabel_user'];
if(empty($_POST['vlabel_group']))
	$_POST['vlabel_group'] = $_SESSION['vlabel_group'];
else
	$_SESSION['vlabel_group'] = $_POST['vlabel_group'];
if(empty($_POST['vlabel_name']))
	$_POST['vlabel_name'] = $_SESSION['vlabel_name'];
else
	$_SESSION['vlabel_name'] = $_POST['vlabel_name'];
if(empty($_POST['vlabel_type']))
	$_POST['vlabel_type'] = $_SESSION['vlabel_type'];
else
	$_SESSION['vlabel_type'] = $_POST['vlabel_type'];
if(empty($_POST['vlabel_chmod']) or !preg_match('!^[0-1]{6}$!',$_POST['vlabel_chmod']))
	$_POST['vlabel_chmod'] = $_SESSION['vlabel_chmod'];
else
	$_SESSION['vlabel_chmod'] = $_POST['vlabel_chmod'];


if(isSet($GLOBALS['label_id'])) {
	//On récupère les informations depuis la base de donnée
	$sql = $GLOBALS['sql'];
	$query = $sql->query('SELECT id, dateC, dateU, type, puzzle, name, userR, userW, otherR, otherW, groupR, groupW, user, `group` FROM '.$GLOBALS['prefix'].'label Where id='.$GLOBALS['label_id']);
	$_infoLabel = $query->fetch(PDO::FETCH_ASSOC);
	$_POST['vlabel_user'] = $_infoLabel['user'];
	$_POST['vlabel_group'] = $_infoLabel['group'];
	//$GLOBALS['label_userR'] = $_infoLabel['userR'];
	//$GLOBALS['label_userW'] = $_infoLabel['userW'];
	//$GLOBALS['label_groupR'] = $_infoLabel['groupR'];
	//$GLOBALS['label_groupW'] = $_infoLabel['groupW'];
	//$GLOBALS['label_otherR'] = $_infoLabel['otherR'];
	//$GLOBALS['label_otherW'] = $_infoLabel['otherW'];
	$_POST['vlabel_name'] = $_infoLabel['name'];
	$_POST['vlabel_type'] = $_infoLabel['type'];
	//$GLOBALS['label_dateC'] = time();
	//$GLOBALS['label_dateU'] = time();
	$_POST['vlabel_chmod'] = $_infoLabel['userR'].$_infoLabel['userW'].$_infoLabel['groupR'].$_infoLabel['groupW'].$_infoLabel['otherR'].$_infoLabel['otherW'];
	
	$_label = array();
	$_label['ZzS'] = $_POST['ZzS'];
	$_label['user'] = $_infoLabel['user'];
	$_label['group'] = $_infoLabel['group'];
	$_label['id'] = $GLOBALS['label_id'];
	$_label['name'] = $_infoLabel['name'];
	$_label['type'] = $_infoLabel['type'];
	$_label['dateC'] = time();
	$_label['dateU'] = time();
	$_label['userW'] = $_infoLabel['userW'];;
	$_label['groupW'] = $_infoLabel['groupW'];
	$_label['otherW'] = $_infoLabel['otherW'];
	$_label['userR'] = $_infoLabel['userR'];
	$_label['groupR'] = $_infoLabel['groupR'];
	$_label['otherR'] = $_infoLabel['otherR'];
}
else {
	/*$_GLOBALS['label_id'] = 0;
	$_GLOBALS['label_user'] = $_POST['vlabel_user'];
	$_GLOBALS['label_group'] = $_POST['vlabel_group'];
	$GLOBALS['label_userR'] = $_POST['vlabel_chmod'][0];
	$GLOBALS['label_userW'] = $_POST['vlabel_chmod'][1];
	$GLOBALS['label_groupR'] = $_POST['vlabel_chmod'][2];
	$GLOBALS['label_groupW'] = $_POST['vlabel_chmod'][3];
	$GLOBALS['label_otherR'] = $_POST['vlabel_chmod'][4];
	$GLOBALS['label_otherW'] = $_POST['vlabel_chmod'][5];
	$GLOBALS['label_name'] = $_POST['vlabel_name'];
	$GLOBALS['label_type'] = $_POST['vlabel_type'];
	$GLOBALS['label_dateC'] = time();
	$GLOBALS['label_dateU'] = time();*/
	$_label = array();
	
	$_label['ZzS'] = $_POST['ZzS'];
	$_label['user'] =  $_POST['vlabel_user'];
	$_label['group'] = $_POST['vlabel_group'];
	$_label['id'] = 0;
	$_label['name'] = $_POST['vlabel_name'];
	$_label['type'] = $_POST['vlabel_type'];
	$_label['dateC'] = time();
	$_label['dateU'] = time();
	$_label['userW'] = $_POST['vlabel_chmod'][1];
	$_label['groupW'] = $_POST['vlabel_chmod'][3];
	$_label['otherW'] = $_POST['vlabel_chmod'][5];
	$_label['userR'] = $_POST['vlabel_chmod'][0];
	$_label['groupR'] = $_POST['vlabel_chmod'][2];
	$_label['otherR'] = $_POST['vlabel_chmod'][4];
}

$_GET['altPuZZle'] = false;
$_label['puZZleName'] = '';

/*
$GLOBALS['label_user'] = intval($_POST['vlabel_user']);
$GLOBALS['label_group'] = intval($_POST['vlabel_group']);
$_GET['label'] = $_POST['vlabel_id'];
$_label = explode('-',$_POST['vlabel_id']);

$GLOBALS['tmpId'] = 1;
//Si le label n'est pas null
if($_label[0] != 0) {
	unset($_SESSION['tmpId1']);
	$GLOBALS['label_id'] = $_label[0];
}*/



/*if(!empty($_POST["tmpId"]))
	$GLOBALS["tmpId"] = $_POST["tmpId"];
else {
	if(empty($_SESSION["tmpId"]))
		$_SESSION["tmpId"] = 1;
	else
		$_SESSION["tmpId"]++;
	$GLOBALS["tmpId"] = $_SESSION["tmpId"];
}*/

?>
<form method="post" style="height: 210px;" action="dev.php?action=devTool">
<textarea name="ZzS" style="width: 50%; height: 200px; float: left">
<?php
$_POST['ZzS'] = stripslashes($_POST['ZzS']);
  echo $_POST['ZzS'];
?>
</textarea>
<div style="margin-left: 51%; padding-top: 0px;">
<label for="name" style="float:left; width: 100px; text-align: right;">Nom :</label><input style="margin-left: 10px;" type="test" size="20" name="vlabel_name" id="name" value="<?php echo @$_POST['vlabel_name']; ?>"/><br/>
<label for="type" style="float:left; width: 100px; text-align: right;">Type :</label><input style="margin-left: 10px;" type="test" size="20" name="vlabel_type" id="type" value="<?php echo @$_POST['vlabel_type']; ?>"/><br/>
<label for="label" style="float:left; width: 100px; text-align: right;">Label :</label><input style="margin-left: 10px;" type="test" size="6" name="vlabel_id" id="label" value="<?php echo @$_POST['vlabel_id']; ?>"/><br/>
<label for="user" style="float:left; width: 100px; text-align: right;">Propriétaire :</label><input style="margin-left: 10px;" type="test"  size="1" name="vlabel_user" id="user" value="<?php echo @$_POST['vlabel_user']; ?>"/><br/>
<label for="group" style="float:left; width: 100px; text-align: right;">Groupe :</label><input style="margin-left: 10px;" type="test" size="1" name="vlabel_group" id="group" value="<?php echo @$_POST['vlabel_group']; ?>"/><br/>
<label for="chmod" style="float:left; width: 100px; text-align: right;">Chmod :</label><input style="margin-left: 10px;" type="test" size="11" name="vlabel_chmod" id="chmod" value="<?php echo @$_POST['vlabel_chmod']; ?>"/><br/><br/>
<input type="submit" value="exécuter"/>
<input type="submit" name="delCache" value="exécuter et vider le cache" />
</div>
</form>
<div style="border: solid black 1px; margin:10px; padding: 5px;clear: left;">
<?php
if(@$_SESSION['ZzS']!=@$_POST['ZzS'] or isSet($_POST['delCache'])) {
    file_put_contents('./ZzG/testPuZZle.php',createStaticpuZZle($_label,$_SESSION['gen']));
}
elseif(!file_exists('./ZzG/testPuZZle.php'))
  file_put_contents('./ZzG/testPuZZle.php',createStaticpuZZle($_label,$_SESSION['gen']));
  
include './ZzG/testPuZZle.php';
$_SESSION['ZzS']=$_POST['ZzS'];
?>
</div>
<?php
echo '<div style="border: solid black 1px; margin:10px; padding: 5px;"><pre>'.@$_SESSION['gen'].'</pre></div>';


?>